package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
 * foreach遍历集合：
 * 1.JDK1.8推出的新的循环，基于lambda表达式遍历
 */
public class ForEachDemo {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("语文",99);
        map.put("数学",100);
        map.put("英语",88);
        map.put("物理",96);
        map.put("化学",99);
        System.out.println(map);  //{物理=96, 数学=100, 化学=99, 语文=99, 英语=88}

        //获取所有键
        /*
        Set<String> keySet = map.keySet();  //获取所有键
        keySet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("key:"+s);
            }
        });
        System.out.println("------------------------");
        // lambda表达式写法：
        keySet.forEach(s -> System.out.println("key:"+s));
        System.out.println("------------------------");

        Collection<Integer> values = map.values();   //遍历所有值
        values.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer v) {
                System.out.println("value:"+v);
            }
        });
        values.forEach(v -> System.out.println("value:"+v));

        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();  //遍历键值对
        entrySet.forEach(e -> System.out.println("key:"+e.getKey()+",value:"+e.getValue()));
*/
        map.forEach((k,v)-> System.out.println("kea:"+k));
        map.forEach((k,v)-> System.out.println("value:"+v));
        map.forEach((k,v)-> System.out.println("key:"+k+",value:"+v));
    }
}
